package com.bltec.emergency.controller.mgrController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.Permission;
import com.bltec.emergency.model.RolePermission;
import com.bltec.emergency.service.mgrService.PermissionService;
import com.bltec.emergency.service.mgrService.RolePermissionService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Resource
    private PermissionService permissionService;
    @Resource
    private UserService userService;
    @Resource
    private RolePermissionService rolePermissionService;

    /**
     * 查询所有权限
     */
    @RequestMapping("getAllPermissions")
    public ResponseJson getAllPermissions() {
        try {
            List<Permission> permissions = permissionService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(permissions));
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }
    /**
     * 查询所有权限
     */
    @RequestMapping("getPermissionIdParentIdName")
    public ResponseJson getPermissionIdParentIdName() {
        try {
            List<Permission> permissions = permissionService.getPermissionIdParentIdName();
            return new ResponseJson(Constant.OK, JSON.toJSON(permissions));
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /**
     * 添加权限
     */
    @RequiresPermissions("permission:insert")
    @RequestMapping("addPermission")
    public ResponseJson addUser(Long parentId, String percode, String href, String name) {
        try {
            Permission permission = new Permission();
            permission.setParentId(parentId);
            permission.setPercode(percode);
            permission.setHref(href);
            permission.setEnable(true);
            permission.setName(name);
            permission.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            permission.setCreateTime(DateUtil.dateNow());
            permissionService.add(permission);
            return new ResponseJson(Constant.OK, "创建权限成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建权限失败" + e.getMessage());
        }
    }

    /**
     * 删除权限
     */
    @RequiresPermissions("permission:delete")
    @RequestMapping("deletePermission")
    public ResponseJson deletePermissionById(Long id) {
        if (null != permissionService.getPermissionById(id)) {
            if (permissionService.deletePermissionById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }

    /**
     * 修改权限
     */
    @RequiresPermissions("permission:update")
    @RequestMapping("updatePermission")
    public ResponseJson updateUser(Long id, Long parentId, String name, String percode, String href) {
        try {
            Permission permission = permissionService.getPermissionById(id);
            permission.setParentId(parentId);
            permission.setName(name);
            permission.setPercode(percode);
            permission.setHref(href);
            permission.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            permission.setUpdateTime(DateUtil.dateNow());
            permissionService.updatePermissionById(permission);
            return new ResponseJson(Constant.OK, JSON.toJSON(permission));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }
    //获取当前角色的所有权限
    @RequestMapping("getPermissionByRoleId")
    public ResponseJson getPermissionByRoleId(Long roleId) {
        try {
            List<Permission> permissions = permissionService.getAllPermissionByRoleId(roleId);
            return new ResponseJson(Constant.OK, JSON.toJSON(permissions));
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }
    //为当前角色添加权限
    @RequestMapping("addRolePermission")
    @RequiresPermissions("permission:insert")
    public ResponseJson addRolePermission(Long roleId,Long[] permissionList) {

        try {
            rolePermissionService.deleteByRoleId(roleId);
            for (Long permission : permissionList) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(permission);
                if (null != rolePermission.getPermissionId()) {
                    if (!rolePermissionService.addPermission(rolePermission)) {
                        return new ResponseJson(Constant.FAIL, "角色权限添加失败");
                    }
                }
            }
            return new ResponseJson(Constant.OK, "角色权限添加成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }
}
